﻿using System;
using System.Collections.Generic;

// 定义一个形状接口
public interface IShape
{
    double CalculateArea();  // 计算面积
}

// 长方形类
public class Rectangle : IShape
{
    public double Length { get; set; }  // 长方形的长度
    public double Width { get; set; }  // 长方形的宽度

    public double CalculateArea()
    {
        return Length * Width;
    }
}

// 正方形类
public class Square : IShape
{
    public double Side { get; set; }  // 正方形的边长

    public double CalculateArea()
    {
        return Side * Side;
    }
}

// 三角形类
public class Triangle : IShape
{
    public double SideA { get; set; }  // 三角形的边长A
    public double SideB { get; set; }  // 三角形的边长B
    public double SideC { get; set; }  // 三角形的边长C

    public double CalculateArea()
    {
        // 使用海伦公式计算三角形的面积
        double p = (SideA + SideB + SideC) / 2;
        return Math.Sqrt(p * (p - SideA) * (p - SideB) * (p - SideC));
    }
}

// 形状工厂类
public class ShapeFactory
{
    public static IShape CreateShape(string shapeType)
    {
        switch (shapeType)
        {
            case "Rectangle":
                return new Rectangle();
            case "Square":
                return new Square();
            case "Triangle":
                return new Triangle();
            default:
                throw new ArgumentException("Invalid shape type");
        }
    }
}

internal class HOMEWORK
{
    static void Main(string[] args)
    {
        List<IShape> shapes = new List<IShape>();
        Random random = new Random();

        for (int i = 0; i < 10; i++)
        {
            string shapeType = GetRandomShapeType();
            IShape shape = ShapeFactory.CreateShape(shapeType);

            if (shape is Rectangle rectangle)
            {
                rectangle.Length = random.Next(1, 10);
                rectangle.Width = random.Next(1, 10);
            }
            else if (shape is Square square)
            {
                square.Side = random.Next(1, 10);
            }
            else if (shape is Triangle triangle)
            {
                triangle.SideA = random.Next(1, 10);
                triangle.SideB = random.Next(1, 10);
                triangle.SideC = random.Next(1, 10);
            }

            shapes.Add(shape);
        }

        double totalArea = 0;
        foreach (IShape shape in shapes)
        {
            totalArea += shape.CalculateArea();
        }

        Console.WriteLine("这10个形状对象的面积之和为：" + totalArea);
    }

    // 获取随机形状类型
    static string GetRandomShapeType()
    {
        string[] shapeTypes = { "Rectangle", "Square", "Triangle" };
        Random random = new Random();
        int index = random.Next(0, shapeTypes.Length);
        return shapeTypes[index];
    }
}